package com.xcm.controller;

import com.xcm.constant.PageConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.Page;
import com.xcm.model.vo.SingleDeviceReportVO;
import com.xcm.model.vo.SingleProgramDetailReportVO;
import com.xcm.page.PageInfo;
import com.xcm.service.DeviceInfoService;
import com.xcm.service.ProgramJsonService;

/**
 * 报表Controller
 */
@Controller
@RequestMapping(value = "/report")
public class ReportController {
	@Autowired
	private ProgramJsonService programJsonService;
	@Autowired
	private DeviceInfoService deviceInfoService;
	/**
	 * 单个广告详细报表
	 */
	@RequestMapping(value = "/listSingleProgramDetailReport")
	public String listSingleProgramDetailReport(String receiveStartTime, String receiveEndTime, Long programJsonId, Integer pageNum, Integer pageSize, Model model) {
		if (programJsonId == null) {
			if (StringUtils.isBlank(receiveStartTime) || StringUtils.isBlank(receiveEndTime)) {
				model.addAttribute("total", 0);
				model.addAttribute("pageNum", PageConstant.DEFAULT_PAGE_NUM);
				model.addAttribute("pageSize", PageConstant.DEFAULT_PAGE_SIZE);
				return "single_program_detail_report";
			}
		}
		PageInfo<SingleProgramDetailReportVO> pageInfo = programJsonService.listSingleProgramDetailReportByPage(receiveStartTime, receiveEndTime, programJsonId, pageNum, pageSize);
		model.addAttribute("programDetailList", pageInfo.getList());
		model.addAttribute("total", pageInfo.getTotal());
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		return "single_program_detail_report";
	}
	
	/**
	 * 单个终端的详细报表
	 */
	@RequestMapping(value = "/listSingleDeviceReport")	
	public String listSingleDeviceReport(String startTime, String endTime, String deviceNum, String mac,Integer pageNum, Integer pageSize, Model model) {
		// 无mac地址或机器码默认返回空页面
		if (StringUtils.isBlank(mac) && StringUtils.isBlank(deviceNum)) {
			return "single_device_detail_report";
		}
		Page<SingleDeviceReportVO> pageInfo = deviceInfoService.listSingleDeviceReport(startTime, endTime, deviceNum, mac, pageNum, pageSize);
		model.addAttribute("deviceDetailList", pageInfo.getResult());
		model.addAttribute("total", pageInfo.getTotal());
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		return "single_device_detail_report";
	}
}
